// pages/order/order.js
let {
    getorderlist,
    postorderlist,
    deletorderlist,
    putorderlist
} = require("../../api/car");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        orderLists: null,
        index: 0,
        evaluate: false,
        slide:null,
        statusX: 0,
    },




    // 修改下标
    setIndex(event) {
        let index = event.target.dataset.index;
        this.setData({
            index: index
        })
    },

    // 前往支付
    PassRay(event) {
        console.log("PassRay", event.target.dataset.orderid);

        // 获取特定id
        let PassRay = event.target.dataset.orderid;
        // console.log(PassRay);

        this.infoData(PassRay, "前往支付");
    },

    // 催发货
    PassShipments(event) {

        console.log("PassShipments", event.target.dataset.orderid);

        // 获取特定id
        let PassRay = event.target.dataset.orderid;
        // console.log(PassRay);
        this.infoData(PassRay, "催发货")

    },

    // 确认收货
    AffirmShipments(event) {

        console.log("AffirmShipments", event.target.dataset.orderid);

        // 获取特定id
        let PassRay = event.target.dataset.orderid;

        this.infoData(PassRay, "确认收货");


    },

    // 交易完成
    PassDeal(event) {


        // 获取特定id
        let PassRay = event.target.dataset.orderid;

        // 拉取数据
        // let lists = wx.getStorageSync('goodsOrderList');

        // 获取对应下标
        // let i = lists.findIndex(item => item.orderId == PassRay)

        this.infoData(PassRay, "交易完成");

    },
    // 删除
    DeleteBtn(e){
        deletorderlist(e.target.dataset.id).then(data=>{
            console.log(data);
            this.onShow()
        })
        // console.log(e);
    },


    //   数据请求方法(各个数据)
    infoData(PassRay, text) {
        // 拉取数据
        getorderlist().then(data => {
            let infodata = data.data
            let orderLists = []
            infodata.forEach(item => {
                orderLists.push({
                    id: item.id,
                    orderId: Number(item.orderId),
                    orderLists: JSON.parse(item.orderLists),
                    address: JSON.parse(item.address),
                    total: item.total,
                    type: item.type
                })
            });
            let lists = orderLists
            console.log(lists);
            // 获取对应下标
            let i = lists.findIndex(item => item.orderId == PassRay) //获取数据
            console.log(i);
            let type_text = text == "前往支付" ? "支付商品" : text == "催发货" ? "是否提醒仓库发货" : text == "确认收货" ? "是否确定收货" : text == "交易完成" ? "是否评论" : "空返回"
            let index_type = text == "前往支付" ? 2 : text == "催发货" ? 3 : text == "确认收货" ? 4 : text == "交易完成" ? 0 : ""
            console.log(type_text, index_type);
            this.Data_Dispose(type_text, i, index_type, lists)
        })



    },

    // 点击催发货、收货的方法处理
    Data_Dispose(type_text, i, index_type, lists) {

        wx.showModal({
            title: type_text,
            success: (res) => {
                // console.log(res);
                if (res.confirm) {

                    let put = {
                        id: lists[i].id,
                        type: index_type
                    }
                    // console.log(put);
                    //修改
                    putorderlist(put).then(data => {
                        console.log(data);
                        this.onShow()
                    })


                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
    },





    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log("options", options);
        this.setData({
            index: options.index
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        getorderlist().then(data => {
            let infodata = data.data
            let orderLists = []
            infodata.forEach(item => {
                orderLists.push({
                    id: item.id,
                    orderId: Number(item.orderId),
                    orderLists: JSON.parse(item.orderLists),
                    address: JSON.parse(item.address),
                    total: item.total,
                    type: item.type
                })
                // console.log("获取orderId",item.orderId);
            });
            console.log("获取数据",orderLists);
            this.setData({
                orderLists: orderLists
            })
        })



    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        // 关闭所有页面，打开到应用内的某个页面
        // wx.reLaunch({
        //   url: '/pages/my/my',
        // })
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})